<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends MY_Controller {

    private $auth;
    /*public function __construct(){
        parent:: __contruct();

    }*/

    public function index(){//-->home_page
        //$this->auth = $this->my_auth->check();
        //print_r($this->auth);
        $this->login();


    }
    public function login(){

        //if($this->auth !=NULL) $this->my_string->php_redirect(base_url().'backend/auth/home/');
        $data['seo']['title']='Dang nhap vao he thong';
        if ($this->input->post('login')){
            $_post = $this->input->post('data');
            $data['data']['_post'] = $_post;
            $this->form_validation->set_error_delimiters('<li>','</li>');
            $this->form_validation->set_rules('data[username]','Tên sử dụng', 'trim|required|min_length[3]|max_length[20]|regex_match[/^([a-z0-9_])+$/i]|callback__username');
            $this->form_validation->set_rules('data[password]','Mật khẩu', 'trim|required|callback__password['.$_post['username'].']');
            if($this->form_validation->run() ==TRUE){
                $user= $this->db->select('ten_nguoidung,matkhau,chuoi_mahoa,ma_loainguoidung')->where(array('ten_nguoidung'=>$_post['username']))->from('nguoidung')->get()->row_array();
                if($user['ma_loainguoidung']==1){
                    $this->my_string->js_redirect('Đăng nhập thành công', base_url().'backend/auth/home/');
                }
                else $this->my_string->js_redirect('Đăng nhập ko thành công', base_url().'backend/auth/login/');
            }
        }
        $data['template'] = 'backend/layout/login';
        $this->load->view('backend/login', isset($data)? $data:Null);
    }
    public function _username($username){
        $count=$this->db->where(array('ten_nguoidung'=>$username))->from('nguoidung')->count_all_results();
        if($count==0){
            $this->form_validation->set_message('_username','%s không tồn tại');
            return FALSE;
        }
        return TRUE;
    }
    public function _password($password,$username){
        if($this->_username($username)==TRUE){
            $user=$this->db->select('ten_nguoidung,matkhau,chuoi_mahoa')->where(array('ten_nguoidung'=>$username))->from('nguoidung')->get()->row_array();
            $password=$this->my_string->encryption_password($user['ten_nguoidung'],$password,$user['chuoi_mahoa']);
        }
        if($password != $user['matkhau']){
            $this->form_validation->set_message('_password','%s không hợp lệ');
            return FALSE;
        }
        return TRUE;
    }
    public function forgot(){
        $data['seo']['title']='Quen mat khau';
        $data['seo']['keywords']='';
        $data['seo']['description']='';
        $data['template'] = 'backend/layout/forgot';
        $this->load->view('backend/login', isset($data)? $data:Null);
    }

   public function create_manage(){
        $data['seo']['title']='Tao tai khoan QT';
        if ($this->input->post('create')){
            $_post = $this->input->post('data');
            $data['data']['_post'] = $_post;
            $this->form_validation->set_error_delimiters('<li>','</li>');
            $this->form_validation->set_rules('data[ten_nguoidung]','Tên sử dụng', 'trim|required|min_length[3]|max_length[20]|regex_match[/^([a-z0-9_])+$/i]');
            $this->form_validation->set_rules('data[matkhau]','Mật khẩu', 'trim|required');
            $this->form_validation->set_rules('data[email]','Email', 'trim|required|valid_email');
            if($this->form_validation->run() ==TRUE){
                $_post=$this->my_string->allow_post($_post, array('ten_nguoidung','matkhau','email'));
                //$_post['ten_nguoidung']=$data['username'];
                $_post['chuoi_mahoa']=$this->my_string->random(69, TRUE);
                $_post['matkhau']=$this->my_string->encryption_password($_post['ten_nguoidung'],$_post['matkhau'],$_post['chuoi_mahoa']);
                $_post['ngay_dangky']= gmdate('Y-m-d H', time()+7*3600);
                $_post['ma_loainguoidung'] = '1';
                var_dump($_post);
                $this->db->insert('nguoidung',$_post);
            }
        }
        $data['template'] = 'backend/layout/create_manage';
        $this->load->view('backend/login', isset($data)? $data:Null);
    }
}